package com.example.yuanann.vegetable_app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yuanann.vegetable_app.MainApplication;
import com.example.yuanann.vegetable_app.R;
import com.example.yuanann.vegetable_app.constant.UserTypeEnum;
import com.example.yuanann.vegetable_app.repository.AppDatabase;
import com.example.yuanann.vegetable_app.repository.dao.UserDao;
import com.example.yuanann.vegetable_app.repository.entity.User;
import com.example.yuanann.vegetable_app.util.GsonUtil;
import com.example.yuanann.vegetable_app.util.LogUtil;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * 登录页面
 */
public class Login extends AppCompatActivity {
    Button loginButton;
    EditText e1,e2;
    TextView b2;
    String n,p;
    CheckBox t;
    RadioGroup chooseUserTypeGroup;

    /**
     * normal：普通用户
     * merchant: 商户
     */
    private String userType = "normal";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置当前页面的布局初始化组件
        setContentView(R.layout.activity_login);
        MainApplication.setContext(getApplicationContext());
        loginButton = (Button) findViewById(R.id.b1);
        b2 = (TextView) findViewById(R.id.b2);
        e1 = (EditText) findViewById(R.id.edit1);
        e2 = (EditText) findViewById(R.id.edit2);
        t = (CheckBox) findViewById(R.id.save);
        chooseUserTypeGroup = findViewById(R.id.radioChooseUserTypeGroup);
        loginButton.setOnClickListener(view -> {
            String n = e1.getText().toString();
            String p = e2.getText().toString();
            if (TextUtils.isEmpty(n)) {
                Toast.makeText(getApplicationContext(), "请输入用户名", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(p)) {
                Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
            } else if (!isExistN(n)) {
                Toast.makeText(getApplicationContext(), "用户名不存在", Toast.LENGTH_SHORT).show();
            } else if (p.equals(isExistP(n))) {
          // if(true) {
//                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                AppDatabase db = AppDatabase.getDatabase(MainApplication.getContext());
                UserDao userDao = db.userDao();
                userDao.getOneByUserNamePasswordUserType(n, p, userType)
                        .subscribeOn(Schedulers.single())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(item -> {
                            LogUtil.info("登录成功");
                            LogUtil.info(GsonUtil.gsonString(item));
                            if(item == null){
                                LogUtil.info("登录失败");
                                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
                                e1.setText("");
                                e2.setText("");
                                return;
                            }
                            Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setClass(Login.this, Loading.class);
                            SharedPreferences sp = getSharedPreferences("name", MODE_PRIVATE);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("name", n);
                            editor.putString("userType", userType);
                            editor.commit();
                            String a = e1.getText().toString();
                            String b = e2.getText().toString();
                            intent.putExtra("name",a);
                            intent.putExtra("userType", userType);
                            intent.putExtra("psw",b);
                            MainApplication.setUserInfo(item);
                            startActivity(intent);
                            e1.setText("");
                            e2.setText("");
                        },throwable -> {
                            LogUtil.info(throwable.toString());
                            Toast.makeText(getApplicationContext(), "登录失败,密码错误", Toast.LENGTH_SHORT).show();
                            e1.setText("");
                            e2.setText("");
                        },() -> {
                            Toast.makeText(getApplicationContext(), "登录失败,sqlite没有该用户", Toast.LENGTH_SHORT).show();
                            e1.setText("");
                            e2.setText("");
                        });
                /*Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(Login.this, Loading.class);
                SharedPreferences sp = getSharedPreferences("name", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("name", n);
                editor.putString("userType", userType);
                editor.commit();
                String a = e1.getText().toString();
                String b = e2.getText().toString();
                intent.putExtra("name",a);
                intent.putExtra("userType", userType);
                intent.putExtra("psw",b);
                MainApplication.setUserInfo(new User());
                MainApplication.getUserInfo().setUserName(a);
                MainApplication.getUserInfo().setUserType(userType);
                startActivity(intent);
                e1.setText("");
                e2.setText("");*/
            } else {
                Toast.makeText(getApplicationContext(), "登录失败,密码错误", Toast.LENGTH_SHORT).show();
                e1.setText("");
                e2.setText("");
            }
        });
        b2.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(Login.this, Register.class);
            startActivity(intent);
        });
        e2.setOnClickListener(view -> {
            String na = e1.getText().toString();
            if (isExistName(na) ==true) {
                SharedPreferences sp = getSharedPreferences("save", MODE_PRIVATE);
                String b = sp.getString(na, null);
                e2.setText(b);
            }
        });
        t.setOnClickListener(view -> {
            String a = e1.getText().toString();
            String b = e2.getText().toString();
            if (t.isChecked()) {
                SharedPreferences sp = getSharedPreferences("save", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString(a, b);
                editor.putString("name", a);
                editor.putString("userType", userType);
                editor.commit();
            }
            else if(!t.isChecked()) {
                SharedPreferences sp = getSharedPreferences("save",MODE_PRIVATE);
                sp.edit().remove(a).commit();
            }
        });
        chooseUserTypeGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if(checkedId == R.id.radioNormalUser){
                // 普通用户
                this.userType = "normal";
            }else{
                // 商户
                this.userType = "merchant";
            }
        });
    }

    private boolean isExistName(String name) {
        SharedPreferences sp=getSharedPreferences("save", MODE_PRIVATE);
        return sp.contains(name);
    }

    private boolean isExistN(String name) {
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.contains(name);
    }

    private String isExistP(String name){
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        String pw = sp.getString(name, null);
        return pw;
    }
}
